<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SVN修改密码</title>

    <link rel="stylesheet" type="text/css" href="resources/modify.css"/>
	<script type="text/javascript" src="resources/jquery-1.7.1.min.js"></script>
</head>
<body>
<div id="login_frame">

    <p id="image_logo"><img src="/resources/clggxxx.png" height="80" width="500"></p>

    <form enctype='application/json' method="post" action="/modify" id="modify_form" onsubmit='return checkForm()'>

        <p><label class="label_input">用户名</label><input type="text" id="Username" required="required" minlength="3" maxlength="26" name="Username" class="text_field"/></p>
        <p><label class="label_input">旧密码</label><input type="password" id="OldPasswd" required="required" minlength="3" maxlength="16" name = "OldPasswd" class="text_field"/></p>
		<p><label class="label_input">新密码</label><input type="password" id="NewPasswd" required="required" minlength="3" maxlength="16"name = "NewPasswd" class="text_field"/></p>
		<p><label class="label_input">重复新密码</label><input type="password" id="ReNewPasswd" required="required" minlength="3" maxlength="16" name = "ReNewPasswd" class="text_field"/></p>
        <div id="modify_control">
            <input type="submit" id="btn_modify" value="修改密码" />
        </div>
    </form>
    </div>
</div>

</body>
<script>
var isModify = false;
$(function(){

	//获取网页中所有的form表单
	$("form").each(function(){
		//注册表单的提交事件
		$(this).submit(function(event) {
		  //屏蔽表单的注册
	      event.preventDefault();
	      //获取url
	      var url = $(this).attr("action");
		  if(!isModify)
		  {
			return;
		  }
	      request(url,
	    		  'POST',
	    		  JSON.stringify($(this).serializeObject()),
	    		  function(datas){
	                alert(datas['State'])
                  },
	    		  function(){alert('提交失败');});
		});
	});

	});



//将$.ajax函数 转化成一个简单的接口
function request(url, method, param, callback, fail){
		//alert(param);
    	$.ajax({
		        type: method,
		       	//async:false,
		        contentType: "application/json;charset=UTF-8",
		        dataType: "json",
		        url: url,
		        data: param,
		        success: function(datas){
		         alert(datas['State'])
                },
		        error: fail,
		    });
    	}

//将form表单里面的数据转化为json对象
$.fn.serializeObject = function()
        {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };

//再用JSON.stringify()函数将json对象转化为json字符串，就转化出去了
function checkForm(){
	var obj=document.getElementById("modify_form");
	var Username = document.getElementById("Username");
    var OldPasswd = document.getElementById("OldPasswd");
	var NewPasswd = document.getElementById("NewPasswd");
	var ReNewPasswd = document.getElementById("ReNewPasswd");
	isModify = false;
    if (Username.value == "") {
        alert("请输入用户名");
		return false;
    } else if (OldPasswd.value  == "") {
        alert("请输入旧密码");
		return false;
	}else if (NewPasswd.value == ""){
		alert("请输入新密码");
		return false;
	}else if(ReNewPasswd.value == ""){
		alert("请输入确认密码");
		return false;
	}else if(NewPasswd.value != ReNewPasswd.value){
		alert("两次输入密码不一致！！！");
		return false;
    }else if(OldPasswd.value == NewPasswd.value){
		alert("旧密码与新密码不可一致！！！");
		return false;
	}else {
		isModify = true;
		return true;
    }
	return false;
}
</script>
</html>